package com.jic.interaction.JDVOP.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jic.interaction.JDVOP.common.utils.JdVopHttpUtil;
import com.jic.interaction.JDVOP.common.utils.RestResult;
import com.jic.interaction.JDVOP.model.enyity.order.*;
import com.jic.interaction.JDVOP.model.vo.request.order.OrderDetailsRequest;
import com.jic.interaction.JDVOP.model.vo.request.order.OrderSubmitRequest;
import com.jic.interaction.JDVOP.model.vo.request.order.OrderWaybillInformation;
import com.jic.interaction.JDVOP.model.vo.response.order.CheckCalendarResponse;
import com.jic.interaction.JDVOP.model.vo.response.order.CheckNewOrderResponse;
import com.jic.interaction.JDVOP.model.vo.response.order.ConfirmInfoResponse;
import com.jic.interaction.JDVOP.model.vo.response.order.OrderSubmitResponse;
import com.jic.interaction.JDVOP.service.OrderService;
//import com.jic.order.mapper.OrderMapper;
//import com.jic.order.mapper.OrderParentMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.annotation.Reference;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * @ClassName OrderServiceImpl
 * TODO
 * @Description JD订单接口实现类
 * @Author chenhongfeng
 * @Date 2020/11/15 16:01
 * @Version 1.0
 **/
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    //注入本地订单父表mapper
/*    @Reference
    private OrderParentMapper orderParentMapper;

    //注入本地订单表mapper
    private OrderMapper orderMapper;*/

    @Value("${route.JDVOP}")
    private String url;

    /*
     * @Author chenhongfeng
     *@Package com.jic.interaction.JDVOP.service.impl
     *ClassName OrderServiceImpl
     * @Description  查询运费 //TODO
     * @Date 16:40 2020/11/15
     * @Param orderFreight
     * @return OrderFreight
     * @Version:1.0
     **/
    @Override
    public String selectFreightService(OrderFreight orderFreight) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        log.info("进入了OrderServiceImpl 入参为{}", JSON.toJSONString(orderFreight));
        String url=this.url+"api/order/getFreight";
        Map<String, String> map = org.apache.commons.beanutils.BeanUtils.describe(orderFreight);
        String requestPost = JdVopHttpUtil.doRequestPost(url,map );
        return requestPost;
    }

    /**
     * 查询预约日历
     * @param request
     * @return
     */
    @Override
    public CheckCalendarResponse selectPromiseCalendar(CheckCalendar request) {
        log.info("进入了OrderServiceImpl 入参为{}", JSON.toJSONString(request));
        String url=this.url+"api/order/promiseCalendar";
        Map<String, String> map = null;
        CheckCalendarResponse checkCalendarResponse=null;
        try {
            map = org.apache.commons.beanutils.BeanUtils.describe(request);
            String requestPost = JdVopHttpUtil.doRequestPost(url,map );
           checkCalendarResponse = JSON.parseObject(requestPost, CheckCalendarResponse.class);

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

        return checkCalendarResponse;
    }


    /*
     * @Author chenhongfeng
     *@Package com.jic.interaction.JDVOP.service.impl
     *ClassName OrderServiceImpl
     * @Description  订单提交//TODO
     * @Date 17:39 2020/11/15
     * @Param OrderSubmit
     * @return Result
     * @Version:1.0
     **/
    @Override
    public OrderSubmit selectSubmitOrder(OrderSubmit submit) {
        String url=this.url+"api/order/submitOrder";
        log.info("开始 入参为{}", JSON.toJSONString(submit));
        log.info("结束，获取返回值：");
        return null;
    }

    /*
     * @Author chenhongfeng
     *@Package com.jic.interaction.JDVOP.service.impl
     *ClassName OrderServiceImpl
     * @Description 反查订单 //TODO
     * @Date 17:59 2020/11/15
     * @Param
     * @return
     * @Version:1.0
     **/
    @Override
    public String selectJdOrderIdByThirdOrderr(ContrastOrder result) {
        String url=this.url+"api/order/selectJdOrderIdByThirdOrder";
        log.info("开始 入参为{}", JSON.toJSONString(result));

        Map<String, String> map = null;
        try {
            map = org.apache.commons.beanutils.BeanUtils.describe(result);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        String requestPost = JdVopHttpUtil.doRequestPost(url,map );
        log.info("结束，获取返回值：");
        return requestPost;
    }
    /*
     * @Author chenhongfeng
     *@Package com.jic.interaction.JDVOP.service.impl
     *ClassName OrderServiceImpl
     * @Description  确认预占库存订单接口 //TODO
     * @Date 18:11 2020/11/15
     * @Param token 授权时获取的access token    jdOrderId 京东的订单单号(下单返回的父订单号)
     * @return Boolean
     * @Version:1.0
     **/
    @Override
    public Boolean selectConfirmOrder(CampOnOrder result) {
        String url=this.url+"api/order/confirmOrder";
        log.info("开始 入参为{}", JSON.toJSONString(result));
        Map<String, String> map = null;
        try {
          map= org.apache.commons.beanutils.BeanUtils.describe(result);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        String requestPost= JdVopHttpUtil.doRequestPost(url, map);
        if("".equals(requestPost)|| requestPost.equals(null)){
            return false;
        }
        log.info("结束，获取返回值"+JSON.parseObject(requestPost));
        return true;
    }
    /*
     *TODO
     *ClassName OrderServiceImpl
     *@Author chenhongfeng
     *@Package com.jic.interaction.JDVOP.service.impl
     *@Description 取消未确认订单
     *@Date 18:22 2020/11/15
     *@Param token:授权时获取的access  jdOrderId:京东的订单单号(父订单号)
     *@return
     *@Version:1.0
     **/
    @Override
    public RestResult<Boolean> selectCancel(CancelOrder cancelOrder) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        String url=this.url+"api/order/cancel";
        log.info("开始 入参为{}", JSON.toJSONString(cancelOrder));
        String doRequestPost = JdVopHttpUtil.doRequestPost(url, BeanUtils.describe(cancelOrder));
        log.info("返回结果{}", doRequestPost);
        Map requsetMap = JSONObject.parseObject(doRequestPost);
       /* if (requsetMap.get("success").toString()=="true"){
            //根据订单id获取主键id
            long id = orderMapper.getIdByJdOrderId(cancelOrder.getJdOrderId());
            //执行取消未确认订单
            orderMapper.delete(id);
            //父订单联动更新
            long parentId = orderMapper.getParentIdByOrderId(id);
            orderParentMapper.delete(parentId);
            return RestResult.success(true,requsetMap.get("resultCode").toString(), requsetMap.get("resultMessage").toString());
        }*/
        return RestResult.error(false, requsetMap.get("resultCode").toString(), requsetMap.get("resultMessage").toString());
    }
    /*
     *TODO
     *ClassName OrderServiceImpl
     *@Author chenhongfeng
     *@Package com.jic.interaction.JDVOP.service.impl
     *@Description 查询配送信息
     *@Date 11:06 2020/11/16
     *@Param
     *@return
     *@Version:1.0
     **/
    @Override
    public RestResult<String> selectOrderTrack(OrderDelivery request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        String url=this.url+"api/order/orderTrack";
        log.info("进入了OrderServiceImpl 入参为{}", JSON.toJSONString(request));
        String doRequestPost = JdVopHttpUtil.doRequestPost(url, BeanUtils.describe(request));
        log.info("返回结果{}", doRequestPost);
        Map map = JSONObject.parseObject(doRequestPost);
        if (map.get("success").toString()!="true"){
            return RestResult.error(null, map.get("resultCode").toString(), map.get("resultMessage").toString());
        }
        return RestResult.success(map.get("result").toString(), map.get("resultCode").toString(), map.get("resultMessage").toString());
    }

    /*
     *TODO
     *ClassName OrderServiceImpl
     *@Author chenhongfeng
     *@Package com.jic.interaction.JDVOP.service.impl
     *@Description 确认收货  仅适用于厂商直送订单。厂商直送订单可使用此接口确认收货并将订单置为完成状态。
     *@Date 11:10 2020/11/16
     *@Param
     *@return
     *@Version:1.0
     **/
    @Override
    public RestResult<String> selectConfirmReceived(CancelOrder result) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        String url=this.url+"api/order/confirmReceived";
        log.info("进入了OrderServiceImpl 入参为{}", JSON.toJSONString(result));
        String doRequestPost = JdVopHttpUtil.doRequestPost(url, BeanUtils.describe(result));
        log.info("返回结果{}", doRequestPost);
        Map requsetMap = JSONObject.parseObject(doRequestPost);
       /* if (requsetMap.get("success").toString()=="true"){
            //执行订单确认
            if (orderMapper.confirmReceipt(result.getJdOrderId())){
                //联动更新父订单
                orderMapper.coupUpdateParentOrderStatus(result.getJdOrderId());
                return RestResult.success(requsetMap.get("result").toString(), requsetMap.get("resultCode").toString(), requsetMap.get("resultMessage").toString());
            }
            RestResult.error(null,"5000","本地确认失败");
        }*/
        return RestResult.error(null, requsetMap.get("resultCode").toString(), requsetMap.get("resultMessage").toString());
    }
    /*
     *TODO
     *ClassName OrderServiceImpl
     *@Author chenhongfeng
     *@Package com.jic.interaction.JDVOP.service.impl
     *@Description 更新采购单号   更新订单上的PO单号，可选择用于配送单、发票等票面展示。
     *@Date 11:17 2020/11/16
     *@Param
     *@return
     *@Version:1.0
     **/
    @Override
    public Boolean selectSaveOrUpdatePoNo(OrderUpdateNumber result) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        String url=this.url+"api/order/saveOrUpdatePoNo";
        log.info("进入了OrderServiceImpl 入参为{}", JSON.toJSONString(result));
        Map<String, String> map = org.apache.commons.beanutils.BeanUtils.describe(result);
        String requestPost = JdVopHttpUtil.doRequestPost(url,map );
        if(requestPost.equals(null) || "".equals(requestPost)){
            return false;
        }
        log.info("结束，获取返回值"+JSON.toJSONString(requestPost));
        return true;
    }
    /*
     *TODO
     *ClassName OrderServiceImpl
     *@Author chenhongfeng
     *@Package com.jic.interaction.JDVOP.service.impl
     *@Description 查询新建订单列表 查询所有新建的订单列表。可用于核对订单
     *@Date 11:17 2020/11/16
     *@Param
     *@return
     *@Version:1.0
     **/
    @Override
    public CheckNewOrderResponse listCheckNewOrder(CheckNewOrder result) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
       String url=this.url+"api/checkOrder/checkNewOrder";
         log.info("进入了OrderServiceImpl 入参为{}", JSON.toJSONString(result));
        Map<String, String> map = org.apache.commons.beanutils.BeanUtils.describe(result);
        String requestPost = JdVopHttpUtil.doRequestPost(url,map );
        CheckNewOrderResponse checkNewOrderResponse = JSON.parseObject(requestPost, CheckNewOrderResponse.class);
        log.info("结束，获取返回值"+JSON.toJSONString(checkNewOrderResponse));
        return checkNewOrderResponse;
    }
    /*
     *TODO
     *ClassName OrderServiceImpl
     *@Author chenhongfeng
     *@Package com.jic.interaction.JDVOP.service.impl
     *@Description 查询妥投订单列表 查询所有妥投的订单列表。可用于核对订单。
     *@Date 11:27 2020/11/16
     *@Param
     *@return
     *@Version:1.0
     **/
    @Override
    public CheckNewOrderResponse listCheckDlokOrder(CheckNewOrder result) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        String url=this.url+"api/checkOrder/checkDlokOrder ";
        log.info("进入了OrderServiceImpl 入参为{}", JSON.toJSONString(result));

        Map<String, String> map = org.apache.commons.beanutils.BeanUtils.describe(result);
        String requestPost = JdVopHttpUtil.doRequestPost(url,map );
        CheckNewOrderResponse checkNewOrderResponse = JSON.parseObject(requestPost, CheckNewOrderResponse.class);
        log.info("结束，获取返回值"+JSON.toJSONString(checkNewOrderResponse));
        return checkNewOrderResponse;
    }
    /*
     *TODO
     *ClassName OrderServiceImpl
     *@Author chenhongfeng
     *@Package com.jic.interaction.JDVOP.service.impl
     *@Description 查询拒收订单列表 查询所有拒收的订单列表。可用于核对订单。
     *@Date 11:29 2020/11/16
     *@Param
     *@return
     *@Version:1.0
     **/
    @Override
    public CheckNewOrderResponse listCheckRefuseOrder(CheckNewOrder result) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        String url=this.url+"api/checkOrder/checkRefuseOrder ";
        log.info("进入了OrderServiceImpl 入参为{}", JSON.toJSONString(result));

        result.setToken("viyDPUi1lIYWlQeREUrpG8qs6");
        System.out.println("========================="+result.getToken());
        Map<String, String> map = org.apache.commons.beanutils.BeanUtils.describe(result);
        String requestPost = JdVopHttpUtil.doRequestPost(url,map );

        log.info("结束，获取返回值"+JSON.toJSONString(requestPost));
        return null;
    }

    /**
     * 查询完成订单列表
     * @param request
     * @return
     */
    @Override
    public CheckNewOrderResponse listCheckCompleteOrder(CheckNewOrder request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        String url=this.url+"api/checkOrder/checkCompleteOrder";
        log.info("进入了OrderServiceImpl 入参为{}", JSON.toJSONString(request));

        request.setToken("qCvE6v3TfEIEDIuyMrp4D645e");
        System.out.println("========================="+request.getToken());
        Map<String, String> map = org.apache.commons.beanutils.BeanUtils.describe(request);
        String requestPost = JdVopHttpUtil.doRequestPost(url,map );
        CheckNewOrderResponse checkNewOrderResponse = JSON.parseObject(requestPost, CheckNewOrderResponse.class);
        log.info("结束，获取返回值"+JSON.toJSONString(checkNewOrderResponse));
        return checkNewOrderResponse;
    }

    /**
     * 查询配送预计送达时间
     * @param request
     * @return
     */
    @Override
    public String selectPromiseTips(OrderPromiseTips request) {
        String url=this.url+"api/order/getPromiseTips";
        return null;
    }
    /**
     *批量确认收货接口
     * @param request
     * @return
     */
    @Override
    public ConfirmInfoResponse selectBatchConfirmReceived(BatchOrder request) {
        String url=this.url+"api/order/batchConfirmReceived";
        return null;
    }
    /*
    * 提交订单
    * */
    @Override
    public OrderSubmitResponse SubmitOrder(OrderSubmitRequest request) {
        String url=this.url+"api/checkOrder/submitOrder";
        return null;
    }

    /*
     * 查询订单详情
     * */
    @Override
    public RestResult<String> selectOrderDetails(OrderDetailsRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        //https://bizapi.jd.com/api/order/selectJdOrder
        String url=this.url+"api/order/selectJdOrder";
        log.info("进入了OrderServiceImpl 入参为{}", JSON.toJSONString(request));
        String doRequestPost = JdVopHttpUtil.doRequestPost(url, BeanUtils.describe(request));
        log.info("返回结果{}", doRequestPost);
        Map requsetMap = JSONObject.parseObject(doRequestPost);
        if (requsetMap.get("success").toString()!="true"){
            return RestResult.error(null, requsetMap.get("resultCode").toString(), requsetMap.get("resultMessage").toString());
        }
        return RestResult.success(requsetMap.get("result").toString(), requsetMap.get("resultCode").toString(), requsetMap.get("resultMessage").toString());
    }

    /*
     * 填写运单信息
     * */

    @Override
    public RestResult<String> updateSendInfo(OrderWaybillInformation request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        //https://bizapi.jd.com/api/afterSaleNew/updateSendInfo
        String url=this.url+"api/afterSaleNew/updateSendInfo";
        log.info("进入了OrderServiceImpl 入参为{}", JSON.toJSONString(request));
        String doRequestPost = JdVopHttpUtil.doRequestPost(url, BeanUtils.describe(request));
        log.info("返回结果{}", doRequestPost);
        Map requsetMap = JSONObject.parseObject(doRequestPost);
        if (requsetMap.get("success").toString()!="true"){
            return RestResult.error(null, requsetMap.get("resultCode").toString(), requsetMap.get("resultMessage").toString());
        }
        return RestResult.success(requsetMap.get("result").toString(), requsetMap.get("resultCode").toString(), requsetMap.get("resultMessage").toString());
    }


}
